﻿using System;
using System.Collections.Generic;
using SoftwareLogistics.SharepointClient.Models;
using System.Runtime.Serialization;
using SoftwareLogistics.SharepointClient.Library.Models;

namespace SoftwareLogistics.SharepointClient.Library
{
    public class Common
    {
        public static bool IsWP7 { get; set; }

        public static void Init(bool isWP7)
        {
            IsWP7 = isWP7;
            AuthService = new SPAuth();
            if(NavDictionary == null)
                NavDictionary = new Dictionary<Guid, SPFormData>();

            ListMetaData = new Dictionary<string, SPList>();
        
            LoadStateBag();
        }

        //TODO: Customize the WebId
        public static Guid WebId = new Guid("6de8378a-146a-440d-898a-923931f69f53");

        //TODO: Customize the root side address including protocol
        public static string SiteAddress = "http://slsys.homeip.net:8000";

        //TODO: Customize the name of the sub Web
        public static string SubWeb = "pm";

        //TODO: Customize the names of the lists you want to download the meta-data
        public static string[] Lists = { "Tasks", "Project", "Time", "Clients", "Project Portfolio" };

        public static Uri SPListUri
        {
            get { return new Uri(String.Format("{0}//{1}//_vti_bin//lists.asmx", SiteAddress, SubWeb.Length > 0 ? "/" + SubWeb : "")); }
        }

        //new Uri("http://slsys.homeip.net:8000///path/_vti_bin/Authentication.asmx")

        public static Uri SPAuthUri
        {
            get { return new Uri(String.Format("{0}/_vti_bin/Authentication.asmx", SiteAddress)); }
        }

        private const string STR_StateBagFileName = "StateBag.dat";
        public static Dictionary<string, List<SPQuery>> Filters;
        public static Services.Sync SyncService = new Services.Sync();
        public static Models.SPUsers Users = new Models.SPUsers();
        public static SPAuth AuthService = new SPAuth();
        public static Dictionary<Guid, SPFormData> NavDictionary { get; set; }
        public static Dictionary<string, SPList> ListMetaData { get; private set; }

        public static string SPUnescape(string fieldName)
        {
            return fieldName.Replace("_x0020_", " ");
        }

        public static Guid AddToNavDictionary(SPFormData o)
        {
            var id = Guid.NewGuid();
            NavDictionary.Add(id, o);

            return id;
        }

        public static string GetSPDateFormat(string date)
        {
            return GetSPDateFormat(DateTime.Parse(date));
        }

        public static string GetSPDateFormat(DateTime date)
        {
            return date.ToString("yyyy-MM-dd 00:00:00");
        }

        public static string GetSPDateFormat(DateTime? date)
        {
            if (date.HasValue)
                return GetSPDateFormat(date.Value);
            else
                return string.Empty;
        }

        public static T GetFromNavDictionary<T>(Guid g) where T : class
        {
            return NavDictionary[g] as T;
        }

        public static void RemoveFromNavDictionary(Guid g)
        {
            NavDictionary.Remove(g);
        }


        public const string SP_NS = "http://schemas.microsoft.com/sharepoint/soap/";

        public static Dictionary<string, string> StateBag { get; set; }

        public static void LoadStateBag()
        {
            var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            if (store.FileExists(STR_StateBagFileName))
            {
                using (var file = store.OpenFile(STR_StateBagFileName, System.IO.FileMode.Open))
                {
                    var serializer = new DataContractSerializer(typeof(Dictionary<string, string>));
                    StateBag = serializer.ReadObject(file) as Dictionary<string, string>;
                }
            }
            else
                StateBag = new Dictionary<string, string>();
        }

        public static void SaveStateBag()
        {
            var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

            using (var file = store.CreateFile(STR_StateBagFileName))
            {
                var serializer = new DataContractSerializer(typeof(Dictionary<string, string>));
                serializer.WriteObject(file, StateBag);
            }
        }
    }
}
